﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Security;
using System.Web;
using System.Configuration;

namespace Standard
{
    public class FormsAuthenticationCookies
    {
        public FormsAuthenticationCookies() { }

        /// <summary>
        /// 添加验证票
        /// </summary>
        /// <param name="MemberID">用户ID</param>
        /// <param name="CustomerTime">cookie有效期开始时间</param>
        /// <param name="Expiration">cookie有效时长(单位:分钟)</param>
        public static void AddCookie(string MemberID, DateTime CustomerTime, int Expiration)
        {
            FormsAuthenticationTicket ticket = null;
            if (CustomerTime < DateTime.Now)
                ticket = new FormsAuthenticationTicket(1, MemberID, DateTime.Now, DateTime.Now.AddMinutes(Expiration), true, MemberID, FormsAuthentication.FormsCookiePath);
            else
                ticket = new FormsAuthenticationTicket(1, MemberID, CustomerTime, DateTime.Now.AddMinutes(Expiration), true, MemberID, FormsAuthentication.FormsCookiePath);

            string hash = FormsAuthentication.Encrypt(ticket);
            HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, hash);
            cookie.HttpOnly = true;//cookie不可被脚本访问

            if (ticket.IsPersistent)
                cookie.Expires = ticket.Expiration;
            /*服务器*/
            cookie.Domain = ConfigurationManager.AppSettings["DomainStr"];
            HttpContext.Current.Response.Cookies.Add(cookie);
        }

        /// <summary>
        /// 获取验证票内用户ID
        /// </summary>
        /// <returns></returns>
        public static string GetCookie()
        {
            string result;
            try
            {
                string cookieName = FormsAuthentication.FormsCookieName;
                HttpCookie authCookie = HttpContext.Current.Request.Cookies[cookieName];
                FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(authCookie.Value);
                string dd = authTicket.UserData.ToString();
                result = dd.Split('|')[0];
            }
            catch
            {
                result = "-1";
            }

            return result;

        }

        /// <summary>
        /// 获取验证票内用户名
        /// </summary>
        /// <returns></returns>
        public static string GetUserName()
        {
            string result;
            try
            {
                string cookieName = FormsAuthentication.FormsCookieName;
                HttpCookie authCookie = HttpContext.Current.Request.Cookies[cookieName];
                FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(authCookie.Value);
                string dd = authTicket.UserData.ToString();
                result = dd.Split('|')[1];
            }
            catch
            {
                result = string.Empty;
            }

            return result;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="cookieValue"></param>
        /// <returns></returns>
        public static string GetCookie(string cookieValue)
        {
            if (!string.IsNullOrEmpty(cookieValue))
            {
                FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(cookieValue);
                string MyCookie = authTicket.UserData.ToString();
                return MyCookie;
            }
            else
            {
                return "-1";
            }
        }

        /// <summary>
        /// 读Cookie（用户ID）
        /// </summary>
        /// <returns></returns>
        public static int GetUserIDCookie()
        {
            int result;
            try
            {
                string cookieName = FormsAuthentication.FormsCookieName;
                HttpCookie authCookie = HttpContext.Current.Request.Cookies[cookieName];
                FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(authCookie.Value);
                string dd = authTicket.UserData.ToString();
                result = Convert.ToInt32(dd.Split('|')[0]);
                if (result < 0)
                    result = 0;
            }
            catch
            {
                result = 0;
            }

            return result;
        }

        /// <summary>
        /// 读Cookie（用户类型：1 学生 2 老师）
        /// </summary>
        /// <returns></returns>
        public static int GetUserTypeCookie()
        {
            int result;

            try
            {
                string cookieName = FormsAuthentication.FormsCookieName;
                HttpCookie authCookie = HttpContext.Current.Request.Cookies[cookieName];
                FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(authCookie.Value);
                string dd = authTicket.UserData.ToString();
                result = Convert.ToInt32(dd.Split('|')[1]);
                if (result < 0)
                    result = 0;
            }
            catch
            {
                result = -1;
            }

            return result;
        }
    }
}
